package io.renren.modules.meta.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.renren.modules.subject.entity.BaseSubject;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linyuan
 * @email linyuan@gmail.com
 * @date 2023-08-25 18:40:01
 */
@Data
@TableName("tb_event")
public class EventEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId
    private Long id;
    /**
     * 元事件名称
     */
    private String eventName;

    /**
     * 相关主体
     */
    @TableField(exist = false)
    private List<BaseSubject> subjects;

    /**
     * 元事件时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime eventTime;
    /**
     * 创建人
     */
    private String createdBy;
    /**
     * 描述
     */
    private String description;
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    @TableLogic
    @TableField(value = "is_deleted",fill = FieldFill.INSERT)
    private Integer deleted;
    public void addSubject(BaseSubject subject) {
        if (subjects == null) {
            subjects = new ArrayList<>();
        }
        subjects.add(subject);
        subject.addEvent(this);
    }

    public void removeSubject(BaseSubject subject) {
        if (subjects != null) {
            subjects.remove(subject);
            subject.removeEvent(this);
        }
    }

}
